package service

import (
	"gitee.com/crack007/goose/core/ws"
	"github.com/crack007/go-im/src/common/config"
	"github.com/crack007/go-im/src/common/constant"
	"github.com/crack007/go-im/src/common/model"
	"github.com/crack007/go-im/src/common/util"
)

type userManagerService struct {
	MessageManager *ws.MessageManager
	SessionManager *ws.SessionManager
}

func (m *userManagerService) Broadcast(broadcastModel *model.BroadcastModel) {
	messageModel := model.NewMessageModel()
	messageModel.Data = broadcastModel.Message
	messageModel.Action = broadcastModel.Action
	wsPackage := ws.NewWsPackage(config.WsConfig.MsgType, []byte(messageModel.String()))
	m.MessageManager.GroupSend(wsPackage, func(session *ws.Session) bool {
		return session.Tag == constant.SESSION_USER
	})
}

func (m *userManagerService) Send(messageModel *model.MessageModel) {
	wsPackage := ws.NewWsPackage(config.WsConfig.MsgType, []byte(messageModel.String()))
	sessionId := util.GetSessionId(messageModel.ToUser)
	session := m.SessionManager.GetById(sessionId)
	if session != nil {
		session.Write(wsPackage)
	}
}
